package com.example.apexapp;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.MyViewHolder> {

    private OnItemClickListener onItemClickListener;
    private ArrayList<MainBean.ProductsDTO> arrayList;

    public CommentAdapter(ArrayList<MainBean.ProductsDTO> arrayList) {

        this.arrayList = arrayList;

    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        MainBean.ProductsDTO bean = arrayList.get(position);
        holder.contentTextView.setText(bean.getName());
        holder.timeTextView.setText(bean.getPrice() + "");
        holder.replyTextView.setText( bean.getSellNum() + "");

//动态控制控件的宽高
        /*
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)holder.mainImageView.getLayoutParams();
        int width = (MyApp.get().getWidth() -MyApp.get().dp2Px(32))/2;
        layoutParams.height = width;
        holder.mainRelativeLayout.setLayoutParams(layoutParams);

        /*
        //使用Glide加载网络图片
        RoundedCornersTransform transform = new RoundedCornersTransfrom(MyApp.get(),MyApp.get().dp2Px(8));
        transform.setNeedCorner(true,false,false);
        RequestOptions options new Req
                */

       // Glide.with(MyApp.get()).load(bean.getImgUrlList().get(0)).into(holder.mainImageView);


        holder.mainRelativeLayout.setOnClickListener(view -> {
            if (onItemClickListener != null) {
                onItemClickListener.onClick(position, bean);
            }
        });

        holder.mainRelativeLayout.setOnLongClickListener(view -> {
            if (onItemClickListener != null){
                onItemClickListener.onLongClick(position, bean);
            }
            return false;
        });

    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_main, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public int getItemCount() {

        return arrayList.size();

    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {

        this.onItemClickListener = onItemClickListener;

    }

    public interface OnItemClickListener {

        void onClick(int position, MainBean.ProductsDTO dto);

        void onLongClick(int position, MainBean.ProductsDTO dto);

    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        private final RelativeLayout mainRelativeLayout;
        private final AppCompatTextView nicknameTextView;
        private final AppCompatTextView contentTextView;
        private final AppCompatTextView timeTextView;
        private final AppCompatTextView replyTextView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            mainRelativeLayout = itemView.findViewById(R.id.mainRelativeLayout);
            nicknameTextView = itemView.findViewById(R.id.nicknameTextView);
            contentTextView = itemView.findViewById(R.id.contentTextView);
            timeTextView = itemView.findViewById(R.id.timeTextView);
            replyTextView = itemView.findViewById(R.id.replyTextView);
        }

    }

}



